// pages/home/home.js
import {
  getBannerList,
  getGoodsList,
  getMenuList
} from '../../api/home'

Page({

  mixins: [require('../../Mixins/tabBarTitle.js')],

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    menuList: [],
    goodsList: [],
    // 请求商品列表数据参数
    count: 1,
    limitNumber: 10,
    hasMore: true //判断是否开启上拉下拉
  },

  async getBannerListData() {
    const res = await getBannerList()
    console.log(res);
    this.setData({
      bannerList: res.data
    })
  },

  async getMenuListData() {
    const res = await getMenuList()
    this.setData({
      menuList: res.data
    })
  },

  async getGoodsListData() {
    const res = await getGoodsList({
      count: this.data.count,
      limitNum: this.data.limitNumber
    })
    this.setData({
      goodsList: [...this.data.goodsList, ...res.data],
      hasMore: res.data.length > 0
    }, () => {
      wx.stopPullDownRefresh()
    })
  },

  // 跳转到详情页面
  goToDetail(e) {
    const {
      proid
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../../subPage/gooodsDetail/goodsDetail?proid=${proid}`,
    })
    console.log(e.currentTarget.dataset);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBannerListData(),
      this.getMenuListData(),
      this.getGoodsListData()
  },

  onShow() {
    // const token = wx.getStorageSync('token')
    // if (token) {
    //   wx.setTabBarItem({
    //     index: 3,
    //     text: '我的'
    //   })
    // } else {
    //   wx.setTabBarItem({
    //     index: 3,
    //     text: '未登录'
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

    this.setData({
      hasMore: true,
      goodsList: []
    })
    this.data.count = 1
    this.getGoodsListData()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    this.data.count++;
    if (!this.data.hasMore) return
    this.getGoodsListData()


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})